<?php

namespace app\admin\model\wanlshop;

use think\Model;


class Gift extends Model
{

    

    

    // 表名
    protected $name = 'wanlshop_gift';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'update_time_text',
        'delete_time_text',
        'type_text'
    ];

    const STATUS_NORMAL=1;//展示
    const STATUS_DISABLE=2;//隐藏

    const TYPE_GOODS=1;//商品
    const TYPE_XIE=2;//谢谢惠顾

    /**
     * 获取类型列表
     * */
    static public function getTrueTypeList($type=2,$value=1){
        $data= [
            self::TYPE_GOODS=>'商品',
            self::TYPE_XIE=>'谢谢惠顾',
        ];
        if($type==1){
            return isset($data[$value])?$data[$value]:'';
        }else{
            return $data;
        }
    }

    /**
     * 获取状态列表
     * */
    static public function getTrueStatusList($type=2,$value=1){
        $data= [
            self::STATUS_NORMAL=>'展示',
            self::STATUS_DISABLE=>'隐藏',
        ];
        if($type==1){
            return isset($data[$value])?$data[$value]:'';
        }else{
            return $data;
        }
    }
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function goods()
    {
        return $this->belongsTo('Goods', 'value', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
